package nccloud.web.hrpbctp.busictrl.busictrl.action;

import nc.bs.hrpbm.budget.IBudgetKeywordService;
import nc.itf.hrpbctp.busictrl.busictrl.IBusiCtrlVOService;
import nc.vo.pub.SuperVO;
import nccloud.commons.lang.StringUtils;
import nccloud.framework.core.env.Locator;
import nccloud.framework.core.util.ArrayUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.ui.config.Area;
import nccloud.framework.web.ui.config.ITempletResource;
import nccloud.framework.web.ui.config.PageTemplet;
import nccloud.framework.web.ui.config.TempletQueryPara;
import nccloud.framework.web.ui.model.PageInfo;
import nccloud.web.codeplatform.framework.action.base.BaseAction;
import nccloud.web.codeplatform.framework.action.base.RequestDTO;
import nccloud.web.codeplatform.framework.action.base.RequstParamWapper;
import nccloud.web.codeplatform.framework.action.base.VOTransform;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 类说明：子表查询
 *
 **/
public class QueryBudgetDetailVoAction extends BaseAction {

    @Override
    public Object doAction(IRequest request, RequstParamWapper paramWapper) throws Throwable {

        // 参数接收
        RequestDTO param = VOTransform.toVO(paramWapper.requestString, RequestDTO.class);
        String pk = param.getPk();
        String pageCode = param.getPageCode();
        String formId = param.getFormId();
        String pk_busictrldata = (String) paramWapper.get("pk_busictrldata");

        if (StringUtils.isEmpty(pk_busictrldata)){
            return null;
        }

        StringBuffer querySql = new StringBuffer();
        querySql.append("  pk_busictrldata = '").append(pk_busictrldata).append("' ");

        // 查询模板
        ITempletResource resource = Locator.find(ITempletResource.class);
        TempletQueryPara templetQueryPara = new TempletQueryPara();
        templetQueryPara.setPagecode(pageCode);
        PageTemplet pageTemplet = resource.query(templetQueryPara);
        // 获取当前要查询的页签区域信息
        Area area = pageTemplet.getArea(formId);
        // 获取页签对应的Class对象
        Class childClazz = Class.forName(area.getClazz());

        // 服务调用
        IBusiCtrlVOService service = ServiceLocator.find(IBusiCtrlVOService.class);
        SuperVO[] superVOS = service.queryChildPksByCondition(childClazz, querySql.toString());

        Set<String> codeList = new HashSet<String>();
        String[] allpks = new String[superVOS.length];
        for (int i = 0 ; i < superVOS.length ; i++) {
            allpks[i] = superVOS[i].getPrimaryKey();
            codeList.add((String)superVOS[i].getAttributeValue("cdbillcode"));
        }
        param.setAllpks(allpks);

        // 返回关键字显示
        Map<String, String> keywordDict = ServiceLocator.find(IBudgetKeywordService.class).queryKeywordList(codeList);
        for (SuperVO superVO : superVOS) {
            superVO.setAttributeValue("def1", keywordDict.get((String)superVO.getAttributeValue("cdbillcode")));
        }

        // 数据返回
        return buildResult(param, false, null, superVOS);
    }
}
